package com.example.AIstudy.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.AIstudy.DTO.RecordDTO;
import com.example.AIstudy.domain.RecordDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RecordDetailDao extends BaseMapper<RecordDetail> {

    //插入一条做题的记录
    @Insert("INSERT INTO recorddetail(recordID,exerciseID,stuAnswer,isRight,stuScore,isSubmitted,compCommentID,transCommentID,falseReason,insertTime) " +
            "VALUES (#{recordID},#{exerciseID},#{stuAnswer},#{isRight},#{stuScore},#{isSubmitted},#{compCommentID},#{transCommentID},#{falseReason},#{insertTime})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id",
            keyColumn = "id", resultType = Integer.class, before = false)
    void insertRecordDetail(RecordDetail recordDetail);

    //根据条件查询做题的记录，封装成DTO返回，一般都是根据套题的id来搜素
    @Select("SELECT * FROM recorddetail ${ew.customSqlSegment}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "exercise", column = "exerciseID",
                    one = @One(select = "com.example.AIstudy.dao.ExerciseDao.selectById")),
            @Result(property = "translateDetail", column = "transCommentID",
                    one = @One(select = "com.example.AIstudy.dao.TranslateDao.selectById")),
            @Result(property = "compositionDetail", column = "compCommentID",
                    one = @One(select = "com.example.AIstudy.dao.CompositionDao.selectById"))
    })
    public List<RecordDTO> findRecord(@Param(Constants.WRAPPER) QueryWrapper<RecordDetail> wrapper);

}
